package com.hsq.spzx.manager.utils;

import com.hsq.spzx.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hsq
 * @date 2025-06-04 下午6:16
 */
public class MenuHelper {

    /**
     * 使用递归方法建菜单
     * @param sysMenuList
     * @return
     */
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList){
        List<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu: sysMenuList) {
            if (sysMenu.getParentId() == 0L) {
                trees.add(findChildren(sysMenu, sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param sysMenu
     * @param sysMenuList
     * @return
     */
    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {
        sysMenu.setChildren(new ArrayList<>());
        for (SysMenu s: sysMenuList) {
            if (s.getParentId().longValue() == sysMenu.getId().longValue()) {
                sysMenu.getChildren().add(findChildren(s, sysMenuList));
            }
        }
        return sysMenu;
    }
}
